
//
//  XJLocationSearchView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/27.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJLocationSearchView.h"
@interface XJLocationSearchView()<UISearchBarDelegate>
@property (nonatomic, strong) UIButton *leftCityButton;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UIButton *rightCancelButton;

@end

@implementation XJLocationSearchView
#pragma mark - 一 lazy
- (UIButton *)leftCityButton {
    if (!_leftCityButton) {
        _leftCityButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_leftCityButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_leftCityButton setTitle:@"杭州市" forState:(UIControlStateNormal)];
        [_leftCityButton addTarget:self action:@selector(leftCityButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        _leftCityButton.titleLabel.font = XJFONTSET_R(15);
//        _leftCityButton.backgroundColor = [UIColor redColor];
    }
    return _leftCityButton;
}


- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] init];
        _searchBar.backgroundColor = [UIColor brownColor];
        _searchBar.backgroundColor = WHITE_COLOR;
        _searchBar.placeholder = @"小区/写字楼/学校等";
        [_searchBar setBackgroundImage:[[UIImage alloc] init]];
        _searchBar.delegate = self;
        [_searchBar setImage:[UIImage imageNamed:@"nav_search"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        
        UITextField * searchField=[_searchBar valueForKey:@"_searchField"];
        searchField.backgroundColor = XJCOLOR_RGB(246, 247, 249);
        searchField.layer.masksToBounds = YES;
        searchField.layer.cornerRadius = 18.0;
        UILabel * searchPlaceholderLabel = [searchField valueForKey:@"_placeholderLabel"];
        searchPlaceholderLabel.font = XJFONTSET_R(13);
        [searchField setValue:XJCOLOR_RGB(185, 194, 197) forKeyPath:@"_placeholderLabel.textColor"];
    }
    return _searchBar;
}

- (UIButton *)rightCancelButton {
    if (!_rightCancelButton) {
        _rightCancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_rightCancelButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_rightCancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
        [_rightCancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
//        _rightCancelButton.backgroundColor = [UIColor blueColor];
        
        _rightCancelButton.titleLabel.font = XJFONTSET_R(15);
    }
    return _rightCancelButton;
}

#pragma mark - 二 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
//        self.backgroundColor = [UIColor purpleColor];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.leftCityButton];
    [self addSubview:self.searchBar];
    [self addSubview:self.rightCancelButton];
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat h = self.frame.size.height;

    [self.leftCityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(5);
        make.size.mas_equalTo(CGSizeMake(60, h*0.8));
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftCityButton.mas_right);
        make.right.equalTo(self.rightCancelButton.mas_left);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.rightCancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-5);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(50, h*0.8));
    }];
}

#pragma mark - 四 UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    XJLog(@"输入内容发生改变:%@--%@",searchText,searchBar.text);
    
    if ([self.delegate respondsToSelector:@selector(locationSearchViewDelegateSearchBarTextDidChange:)]) {
        [self.delegate locationSearchViewDelegateSearchBarTextDidChange:searchText];
    }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    XJLog(@"点击了搜索按钮:%@",searchBar.text);
}


#pragma mark - 五 回调事件
- (void)leftCityButtonAction:(UIButton *)button {
    XJLog(@"点击了右边的定位城市");
    
    if ([self.delegate respondsToSelector:@selector(locationSearchViewDelegateClickLeftButtonAction:)]) {
        [self.delegate locationSearchViewDelegateClickLeftButtonAction:button];
    }
}
- (void)cancelButtonAction:(UIButton *)button {
    XJLog(@"取消");
    if ([self.delegate respondsToSelector:@selector(locationSearchViewDelegateClickRightCancelButtonAction:)]) {
        [self.delegate locationSearchViewDelegateClickRightCancelButtonAction:button];
    }
}

#pragma mark - 六 configure
- (void)configureSelectLeftCityTitle:(NSString *)title {
    [self.leftCityButton setTitle:title forState:(UIControlStateNormal)];
}



@end
